package com.yao.club.controller;

import com.netease.yidun.sdk.core.utils.StringUtils;
import com.yao.club.entity.result.ResponseEntity;
import com.yao.club.entity.result.ResultEntity;
import com.yao.club.entity.vo.UserProfileVO;
import com.yao.club.service.impl.FriendsServiceImpl;
import com.yao.club.token.PassToken;
import com.yao.club.token.UserToken;
import com.yao.club.utils.ResponseUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/app/friend")
@Api(value = "好友关系接口", description = "好友关系接口", tags = "好友关系模块")
public class FriendsController {
    @Resource
    HttpServletRequest request;

    @Autowired
    FriendsServiceImpl friendsService;

    @UserToken
    @ApiOperation(value = "关注/取消关注", notes = "关注/取消关注", httpMethod = "GET")
    @GetMapping("/follow")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(dataType = "String", name = "memberId", value = "用户id", required = true)
    })
    public ResponseEntity<Boolean> follow(String memberId){
        String username = (String) request.getAttribute("username");
        if (StringUtils.isEmpty(memberId)) {
            return ResponseUtils.failed("用户ID不能为空");
        }
        return friendsService.follow(memberId,username);
    }
    

    @PassToken
    @ApiOperation(value = "根据昵称搜索用户", notes = "模糊匹配用户昵称")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "nickname", value = "用户昵称", required = true),
            @ApiImplicitParam(name = "pageNum", value = "页码", defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize", value = "每页数量", defaultValue = "10")
    })
    @GetMapping("/searchByNickname")
    public ResponseEntity<ResultEntity<UserProfileVO>> searchUsersByNickname(
            @RequestParam String nickname,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
            
        if (StringUtils.isEmpty(nickname)) {
            return ResponseUtils.failed("昵称不能为空");
        }
        if (pageNum < 1) {
            return ResponseUtils.failed("页码不能小于1");
        }
        if (pageSize < 1 || pageSize > 50) {
            return ResponseUtils.failed("每页数量必须在1-50之间");
        }
        String username = (String) request.getAttribute("username");
        
        return friendsService.searchUsersByNickname(nickname, pageNum, pageSize,username);
    }

}
